*******************************************************************************
* Load data
*******************************************************************************

use dataset_voters, clear
keep if sample == "forthright"

*******************************************************************************
* Put the attributes into macros
*******************************************************************************

local legalexperience legalexperience_2 legalexperience_3
local convictionrate convictionrate_3 convictionrate_4 ///
	convictionrate_5 convictionrate_6 convictionrate_7 convictionrate_8
local marijuanad marijuanad_2 marijuanad_3
local deathpenaltyd deathpenaltyd_2 deathpenaltyd_3
local threestrikesd threestrikesd_2 threestrikesd_3
local ageq ageq_2 ageq_3 ageq_4 ageq_5
local race race_2 race_3 race_4
local education education_2 education_3

#delimit;

local X 
	`legalexperience' 
	`deathpenaltyd'
	`threestrikesd'
	`marijuanad'
	`convictionrate'
	`ageq'
	sex_2
	`race'
	`education'
	;

#delimit cr

*******************************************************************************
* Test for R3
*******************************************************************************

preserve
su ideology
replace ideology = (ideology-r(min))/(r(max)-r(min))
eststo clear
eststo: reg vote ideology `X', cl(responseid)
eststo: reg vote ideology `X' if ada == 0, cl(responseid)
eststo: reg vote ideology `X' if ada == 1, cl(responseid)
eststo: reg vote ada##c.ideology `X', cl(responseid)
esttab, compress se keep(*ideology*)
restore

*******************************************************************************
* Do the regression
*******************************************************************************

reg vote `X', cl(responseid)

*******************************************************************************
* Get the coefficients and SE's for plotting
*******************************************************************************

gen index = _n
gen B = .
gen SE = .
gen varname = ""
local i = 1

foreach x in `X' {
	if !regex("`x'", "convic") {
		if regex("`x'", "2") {
			replace B = 0 if index == `i'
			local z = subinstr("`x'", "2", "1", 1)
			replace varname = "`z'" if index == `i'
			local i = `i' + 1
		}
	}
	if regex("`x'", "convic") {
		if regex("`x'", "3") {
			replace B = 0 if index == `i'
			local z = subinstr("`x'", "3", "1", 1)
			replace varname = "`z'" if index == `i'
			local i = `i' + 1
		}
	}
	replace B = _b[`x'] if index == `i'
	replace SE = _se[`x'] if index == `i'
	replace varname = "`x'" if index == `i'
	local i = `i' + 1	
}

keep if !mi(B)
collapse B SE index, by(varname)

gen UB = B + 1.96*SE
gen LB = B - 1.96*SE

*******************************************************************************
* Label the variables
*******************************************************************************

replace varname = "Assistant DA" if varname == "legalexperience_1"
replace varname = "Defense Attorney" if varname == "legalexperience_2"
replace varname = "Public Defender" if varname == "legalexperience_3"

replace varname = "Marijuana: 0" if varname == "marijuanad_1"
replace varname = "Marijuana: 1" if varname == "marijuanad_2"
replace varname = "Marijuana: 2" if varname == "marijuanad_3"

replace varname = "Death penalty: 0" if varname == "deathpenaltyd_1"
replace varname = "Death penalty: 1" if varname == "deathpenaltyd_2"
replace varname = "Death penalty: 2" if varname == "deathpenaltyd_3"

replace varname = "Three strikes: 0" if varname == "threestrikesd_1"
replace varname = "Three strikes: 1" if varname == "threestrikesd_2"
replace varname = "Three strikes: 2" if varname == "threestrikesd_3"

replace varname = "70%" if varname == "convictionrate_1"
replace varname = "75%" if varname == "convictionrate_3"
replace varname = "80%" if varname == "convictionrate_4"
replace varname = "85%" if varname == "convictionrate_5"
replace varname = "90%" if varname == "convictionrate_6"
replace varname = "95%" if varname == "convictionrate_7"
replace varname = "100%" if varname == "convictionrate_8"

replace varname = "1st" if varname == "ageq_1"
replace varname = "2nd" if varname == "ageq_2"
replace varname = "3rd" if varname == "ageq_3"
replace varname = "4th" if varname == "ageq_4"
replace varname = "5th" if varname == "ageq_5"

replace varname = "Female" if varname == "sex_1"
replace varname = "Male" if varname == "sex_2"

replace varname = "Black" if varname == "race_1"
replace varname = "Asian" if varname == "race_2"
replace varname = "Hispanic" if varname == "race_3"
replace varname = "White" if varname == "race_4"

replace varname = "Ivy League" if varname == "education_1"
replace varname = "small college" if varname == "education_2"
replace varname = "state university" if varname == "education_3"

gen x = length(varname)
su x

replace varname = varname + (r(max)-length(varname))*" "
replace varname = "{fontface monospace:" + varname+ "}"

replace index = index + 1
replace index = index + 1 if index >= 5
replace index = index + 1 if index >= 15
replace index = index + 1 if index >= 23
replace index = index + 1 if index >= 29
replace index = index + 1 if index >= 32
replace index = index + 1 if index >= 37
labmask index, values(varname)

*******************************************************************************
* Plot
*******************************************************************************

local Data = proper("bovitz")

local j = 2
local k = 1

if "bovitz" == "bovitz" {
	local j = 1
	local k = 2
}

#delimit;

gr tw
	(sc index B, msym(O) col(black))
	(sc index B if B == 0, msym(O) mfcol(white) col(black))
	(rspike UB LB index, horiz col(black))
	,
		xline(0, lpat(dash))
		legend(off)
		plotregion(style(none))
		ylab(
		1 
		"{fontface monospace:{bf:Experience      }}" 
		2(1)4
		5 "{fontface monospace:{bf: Issue Distance  }}"
		6(1)14
		15 "{fontface monospace:{bf: Conviction Rate }}"
		16(1)22
		23 "{fontface monospace:{bf: Age Quintile    }}"
		24(1)28
		29 "{fontface monospace:{bf: Sex             }}"
		30 31
		32 "{fontface monospace:{bf: Race            }}"
		33(1)36
		37 "{fontface monospace:{bf: Education       }}"
		38(1)40
			,
			angle(horiz) valuelabel labsize(vsmall)
		)
		yscale(range(1 40) rev outergap(-.2) titlegap(0))
		ytitle("")
		ytitle("")
		xlab(-.3(.1).3)		
		xtitle("")
		xsize(1.75)
		ysize(2)
		name(gbovitz, replace)
		;

#delimit cr

*******************************************************************************
* Output
*******************************************************************************

gr export "_output/figure4.pdf", replace

*******************************************************************************
* End
*******************************************************************************
